package com.rc.saas.tenant.controller.ugs;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.component.UploadImg;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ugs.TenantUgsSuperLevelPrice;
import com.rc.saas.tenant.model.ugs.UgsMemberSuperLevel;
import com.rc.saas.tenant.model.ugs.UgsMemberSuperLevelExample;
import com.rc.saas.tenant.search.ugs.LevelDiscountRecordSearch;
import com.rc.saas.tenant.search.ugs.LevelDiscountStationSearch;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelDiscountRecordService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelDiscountStationService;
import com.rc.saas.tenant.service.ugs.TenantUgsSuperLevelPriceService;
import com.rc.saas.tenant.service.ugs.UgsMemberSuperLevelService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.ugs.LevelDiscountStationResultVo;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelDiscountRecordResultVo;
import com.rc.saas.tenant.vo.ugs.UgsLevelDiscountStationResultVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


/**
 * @author frog
 * @date 2021/4/8
 */
@Controller
@RequestMapping("/tenantUgsSuperLevelPrice")
public class TenantUgsSuperLevelPriceController {

    private static Logger logger = LogManager.getLogger(TenantUgsSuperLevelPriceController.class);

    @Autowired
    private TenantUgsSuperLevelPriceService tenantUgsSuperLevelPriceService;

    @Autowired
    private TenantUgsLevelDiscountStationService tenantUgsLevelDiscountStationService;

    @Autowired
    private TenantUgsLevelDiscountRecordService tenantUgsLevelDiscountRecordService;

    @Autowired
    private UgsMemberSuperLevelService ugsMemberSuperLevelService;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private UploadImg uploadImg;

    /**
     * 跳转超级用户等级页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String tenantUgsLevelTypePage() {
        return "/templates/ugs/tenantUgsSuperLevelPrice";
    }

    /**
     * 返回用户等级列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantUgsSuperLevelPrice", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantUgsSuperLevelPrice(BaseSearch search) {
        PageInfo<TenantUgsSuperLevelPrice> pageInfo = tenantUgsSuperLevelPriceService.listPageTenantUgsSuperLevelPrice(search);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转编辑超级会员
     *
     * @return
     */
    @RequestMapping(value = "/editTenantUgsSuperLevelPrice/{superLevelPriceId}", method = RequestMethod.GET)
    public String editTenantUgsSuperLevelPrice(@PathVariable("superLevelPriceId") Long superLevelPriceId, Model model) {
        TenantUgsSuperLevelPrice tenantUgsSuperLevelPrice = tenantUgsSuperLevelPriceService.selectByPrimaryKey(superLevelPriceId);
        model.addAttribute("tenantUgsSuperLevelPrice", tenantUgsSuperLevelPrice);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        return "/templates/ugs/editTenantUgsSuperLevelPrice";
    }


    /**
     * 保存/编辑超级会员
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantUgsSuperLevelPrice", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantUgsSuperLevelPrice(TenantUgsSuperLevelPrice tenantUgsSuperLevelPrice, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (!BeanUtil.isValid(tenantUgsSuperLevelPrice.getSuperLevelPrice(), tenantUgsSuperLevelPrice.getRemark(),
                tenantUgsSuperLevelPrice.getRemark2())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        try {
            TenantUser user = ShiroUtils.getSessionUser();

            if ("1".equals(tenantUgsSuperLevelPrice.getStatus())) {
                //查询出是否存在已购买的会员
                Date date = new Date();
                UgsMemberSuperLevelExample example = new UgsMemberSuperLevelExample();
                example.createCriteria().andStatusEqualTo("0").andSuperLevelTypeEqualTo(tenantUgsSuperLevelPrice.getSuperLevelType())
                        .andEndTimeGreaterThanOrEqualTo(date).andTenantCodeEqualTo(user.getTenantCode());
                List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelService.selectByExample(example);
                if (!CollectionUtils.isEmpty(ugsMemberSuperLevels)) {
                    return TResponse.FAIL("已存在购买的会员");
                }
            }

            if (file.getSize() != 0) {
                TResponse tResponse = uploadImg.uploadImg(user.getTenantCode(), file);
                if (tResponse.getStatus() == 200) {
                    tenantUgsSuperLevelPrice.setSuperCardImg((String) tResponse.getData());
                } else {
                    return tResponse;
                }
            }

            if (tenantUgsSuperLevelPrice.getSuperLevelPriceId() != null) {
                tenantUgsSuperLevelPriceService.updateByPrimaryKeySelective(tenantUgsSuperLevelPrice);
            } else {
                tenantUgsSuperLevelPriceService.insertSelective(tenantUgsSuperLevelPrice);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存超级会员失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转会员等级折扣设置页面
     *
     * @return
     */
    @RequestMapping(value = "/levelSuperDiscountStation", method = RequestMethod.GET)
    public String levelDiscountStation() {
        return "/templates/ugs/levelSuperDiscountStation";
    }

    /**
     * 返回会员等级折扣列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageLevelSuperDiscountStation", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageLevelDiscountStation(LevelDiscountStationSearch levelDiscountStationSearch) {
        levelDiscountStationSearch.setDiscountType("1");
        PageInfo<LevelDiscountStationResultVo> pageInfo = tenantUgsLevelDiscountStationService.listPageLevelDiscountStation(levelDiscountStationSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转增加超级桩群折扣页面
     *
     * @return
     */
    @RequestMapping(value = "/editSuperStationDiscount/{elecStationId}", method = RequestMethod.GET)
    public String editSuperStationDiscount(@PathVariable("elecStationId") Long elecStationId, Model model) {
        ElecStation elecStation = elecStationService.selectByPrimaryKey(elecStationId);
        model.addAttribute("elecStation", elecStation);
        UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo = tenantUgsLevelDiscountStationService.selectDiscountByType("1", elecStationId);
        model.addAttribute("ugsLevelDiscountStationResultVo", ugsLevelDiscountStationResultVo);
        return "/templates/ugs/editSuperStationDiscount";
    }


    /**
     * 保存/编辑超级桩群折扣
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateSuperStationDiscount", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateSuperStationDiscount(UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo) {
        if ("0".equals(ugsLevelDiscountStationResultVo.getStatus())) {
            if (!BeanUtil.isValid(ugsLevelDiscountStationResultVo.getV1(), ugsLevelDiscountStationResultVo.getV2(),
                    ugsLevelDiscountStationResultVo.getV3(), ugsLevelDiscountStationResultVo.getBeginTime(), ugsLevelDiscountStationResultVo.getEndTime())) {
                return TResponse.FAIL("请输入正确的参数");
            }
        }
        TenantUser user = ShiroUtils.getSessionUser();

        ugsLevelDiscountStationResultVo.setTenantCode(user.getTenantCode());
        ugsLevelDiscountStationResultVo.setDiscountType("1");
        tenantUgsLevelDiscountStationService.saveOrUpdateStationDiscount(ugsLevelDiscountStationResultVo);
        try {
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存桩群折扣失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转会员修改记录页面
     *
     * @return
     */
    @RequestMapping(value = "/superModifyRecord/{elecStationId}", method = RequestMethod.GET)
    public String modifyRecord(@PathVariable("elecStationId") Long elecStationId, Model model) {
        model.addAttribute("elecStationId", elecStationId);
        return "/templates/ugs/superModifyRecord";
    }


    /**
     * 返回会员修改记录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageSupermodifyRecord", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageSupermodifyRecord(LevelDiscountRecordSearch levelDiscountRecordSearch) {
        levelDiscountRecordSearch.setDiscountType("1");
        PageInfo<TenantUgsLevelDiscountRecordResultVo> pageInfo = tenantUgsLevelDiscountRecordService.listPageModifyRecord(levelDiscountRecordSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


}
